const SymptomSub = require("../models/SymptomSub");
const Symptom = require("../models/Symptom");
const SymptomServer = require("./Symptom");
module.exports = class SymptomSubManager {
  /**
   * 增加一个SymptomSub
   * @param {*} obj
   * @returns
   */
  static async add(obj) {
    const result = await SymptomSub.create(obj);
    return result.toJSON();
  }
  /**
   * 批量新增SymptomSub
   * @param {*} obj
   * @returns
   */
  static async bulkCreate(valueArr) {
    const result = await SymptomSub.bulkCreate(valueArr);
    return result;
  }

  /**
   * 批量新增SymptomSub与Symptom
   * @param {*} obj
   * @returns
   */
  static async bulkCreateSym(valueArr) {
    const _this = this;

    const deleteSymRes = await SymptomServer.deleteAll();

    const deleteSymSubRes = await _this.deleteAll();

    const { SymSubs, SymS } = valueArr;

    const SymSubsRes = await _this.bulkCreate(SymSubs);

    const SymSRes = await Symptom.bulkCreate(SymS);

    return {
      SymSubsRes,
      SymSRes,
    };
  }

  /**
   * 删除一个SymptomSub
   * @param {*} id
   */
  static async delete(id) {
    const result = await SymptomSub.destroy({
      where: {
        id,
      },
    });
  }

  /**
   * 删除所有SymptomSub
   * @param {*}
   */
  static async deleteAll() {
    const result = await SymptomSub.destroy({
      where: {},
    });
  }

  /**
   * 更新SymptomSub
   * @param {*} Id
   * @param {*} Obj
   */
  static async update(Id, obj) {
    const result = await SymptomSub.update(obj, {
      where: {
        id: Id,
      },
    });
  }

  /**
   * 根据id查询SymptomSub
   * @returns
   */
  static async getById(id) {
    const result = await SymptomSub.findOne({
      where: {
        id,
      },
    });
    if (result === null) {
      console.log("Not found!");
    } else {
      return JSON.parse(JSON.stringify(result));
    }
  }

  /**
   * 查询所有的SymptomSub
   * @returns
   */
  static async getAll() {
    const result = await SymptomSub.findAll({
      include: [
        {
          model: Symptom,
        },
      ],
    });
    return JSON.parse(JSON.stringify(result));
  }
};
